package com.itwn.portal.service;

import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.itwn.portal.entity.Remark;
import com.itwn.portal.mapper.RemarkMapper;
import com.itwn.portal.vo.RemarkVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author cx
 * @date 2022/3/1
 * @apiNote
 */
@Service
public class RemarkService {
    @Autowired
    RemarkMapper remarkMapper;

    public PageInfo<RemarkVo> queryByPage(Integer now, Integer size, Integer pid,
                                          String comicName, String username,Integer rid){
        PageMethod.startPage(now,size);

        List<RemarkVo> list = this.remarkMapper.selectAllRemark(pid,comicName,username,rid);
        return new PageInfo<>(list);
    }


    public List<RemarkVo> querySecond(Integer rid) {
        return this.remarkMapper.selectSecond(rid);
    }

    public PageInfo<RemarkVo> queryFirstByPage(Integer now, Integer size, Integer rid) {
        PageMethod.startPage(now,size);

        List<RemarkVo> list = this.remarkMapper.selectFirstRemark(rid);
        return new PageInfo<>(list);
    }

    /**
     * 添加评论,子级评论添加后增加父级评论的评论数
     */
    @Transactional
   @CacheEvict(value={"remark"},allEntries = true)
    public int add(Remark remark){
        int a =  this.remarkMapper.insertRemark(remark);
        System.out.println("添加评论是否成功！！！============》"+a);
        if(remark.getParentId()!=null && remark.getParentId() >0){
            System.out.println("添加二级评论的业务中！！！！！！！！！！！");
         int b = this.remarkMapper.updateIncrRemCount(remark.getParentId());
            return a+b;
        }
        return a ;
    }

    public void likeThat(Integer mid) {
        this.remarkMapper.likeThat(mid);
    }

    public PageInfo<RemarkVo> queryHotFirstByPage(Integer now, Integer size, Integer rid) {
        PageMethod.startPage(now,size);

        List<RemarkVo> list = this.remarkMapper.selectHotFirstRemark(rid);
        return new PageInfo<>(list);
    }
}
